﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using intelliEssay.Core;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using System.Text.RegularExpressions;

namespace intelliEssay.TitleChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Translate("三"));

            var match = Regex.Match("2011-------", "^\\s*\\d+(\\.\\d+){0,}\\.{0,1}\\s*([^\\-\\d]+)");
            var wd = WordprocessingDocument.Open(@"C:\Users\llmainland\Desktop\基于OpenXml的Word文档格式检查系统.docx", false);

            Core.Task task = new Core.Task();
            task.FilePath = @"C:\Users\llmainland\Desktop\testfiles\8.docx";
            task.taskType = "nkub";
            task.taskSetting = new Core.TaskSettings()
            {
                StandardFormatDocumentPath =
                    @"E:\Programs\ProjectEssayCompiler\TestDrive\intelliEssay\standard format.docx"
            };
            task.AddAChecker(new intelliEssay.Checkers.TitleChecker.TitleChecker());
            task.Initialize();
            task.Run(TaskRunMode.Console);
        }

        static int ParseCHN(char c)
        {
            switch (c)
            {
                case '一':
                    return 1;
                case '二':
                    return 2;
                case '三':
                    return 3;
                case '四':
                    return 4;
                case '五':
                    return 5;
                case '六':
                    return 6;
                case '七':
                    return 7;
                case '八':
                    return 8;
                case '九':
                    return 9;
                case '十':
                    return 10;
                case '百':
                    return 100;
                case '千':
                    return 1000;
                case '万':
                    return 10000;
                case '亿':
                    return 100000000;
                case '零':
                    return 0;
                default:
                    return -1;
            }
        }

        private static string Translate(string p)
        {
            int currentIdx = -1;
            int currentAccumulator = 0;
            int accumulator = 0;
            int lastIdx = 0;
            for (int i = 0; i < p.Length; i++)
            {
                char currentC = p[i];
                currentIdx = ParseCHN(currentC);
                if (currentIdx == -1) return null;
                if (currentIdx < 10)
                {
                    currentAccumulator = currentIdx;
                }
                else
                {

                    if (currentAccumulator == 0)
                    {
                        currentAccumulator = 1;
                    }
                    if (currentIdx >= lastIdx && lastIdx != 0)
                    {
                        accumulator += currentAccumulator;
                        accumulator *= currentIdx;
                    }
                    else
                    {
                        currentAccumulator *= currentIdx;
                        accumulator += currentAccumulator;
                    }
                    currentAccumulator = 0;
                    lastIdx = currentIdx;
                }
            }
            accumulator += currentAccumulator;
            return accumulator.ToString();
        }
    }
}
